enum dpTorH {
    //% block="温度(℃)"
    getTemperature,
    //% block="湿度(%RH)"
    getHumidity
}

enum dpLed {
    //% block="LED1(D13)"
    13,
    //% block="LED2(D12)"
    12
}


enum dpSwitch {
    //% block="点亮"
    HIGH,
    //% block="熄灭"
    LOW
}


enum dpBTN {
    //% block="按钮SW1(D2)"
    2,
    //% block="按钮SW2(D3)"
    3
}

enum dpONorOFF {
    //% block="被按下"
    on,
    //% block="被松开"
    off
}

enum dprBeat {
    //% block="1/8"
    125,
    //% block="1/4"
    250,
	//% block="1/2"
    500,
    //% block="整拍"
    1000,
	//% block="双拍"
    2000,
    //% block="停止"
    0
}


//% color="#004198" iconWidth=50 iconHeight=40
namespace Sensor9in1 {

    //% block="设置[dpLed][dpSwitch]" blockType="command"
    //% dpLed.shadow="dropdown" dpLed.options="dpLed" dpLed.defl="dpLed.1"
    //% dpSwitch.shadow="dropdown" dpSwitch.options="dpSwitch" dpSwitch.defl="dpSwitch.1"
    export function SetLED(parameter: any, block: any) {
        let dpled = parameter.dpLED.code	
        let dpswitch = parameter.dpSwitch.code
        Generator.addCode(`digitalWrite(${dpled}, ${dpswitch});`);
        }


    //% block="REG LED设置 R(D9):[dpRed]G(D10):[dpGreen]B(D11):[dpBlue]" blockType="command"
    //% dpRed.shadow="dropdown" dpRed.options="dpSwitch" dpRed.defl="1"
	//% dpGreen.shadow="dropdown" dpGreen.options="dpSwitch" dpGreen.defl="1"
	//% dpBlue.shadow="dropdown" dpBlue.options="dpSwitch" dpBlue.defl="1"
    export function SetRGBLED(parameter: any, block: any) {
        let dpred = parameter.dpRed.code	
        let dpgreen = parameter.dpGreen.code
		let dpblue = parameter.dpBlue.code	
        Generator.addCode(`digitalWrite(9, ${dpred});`);
		Generator.addCode(`digitalWrite(10, ${dpgreen});`);
		Generator.addCode(`digitalWrite(11, ${dpblue});`);
	    }


    //% externalFunc
    export function getColorsFunc_() {
        return [
            "#f00","#ff0","#0f0","#0ff","#00f","#f0f","#fff","#000"
        ]
    }
    //% block="设置RGB LED[COLOR]" blockType="command"
    //% COLOR.shadow="colorPalette" COLOR.params.column=4
    //% COLOR.params.colorsFunc="getColorsFunc_"
    export function SetcolorPalette(parameter: any, block: any) {
	    let color = parameter.COLOR.code
        Generator.addCode(`Serial.println(${color});`);
		if(`${color}` == 0xFF0000){
		Generator.addCode(`digitalWrite(9,HIGH);`);
		Generator.addCode(`digitalWrite(10,LOW);`);
		Generator.addCode(`digitalWrite(11,LOW);`);
		}else if(`${color}` == 0xFFFF00){
		Generator.addCode(`digitalWrite(9,HIGH);`);
		Generator.addCode(`digitalWrite(10,HIGH);`);
		Generator.addCode(`digitalWrite(11,LOW);`);
		}else if(`${color}` == 0x00FF00){
		Generator.addCode(`digitalWrite(9,LOW);`);
		Generator.addCode(`digitalWrite(10,HIGH);`);
		Generator.addCode(`digitalWrite(11,LOW);`);
		}else if(`${color}` == 0x00FFFF){
		Generator.addCode(`digitalWrite(9,LOW);`);
		Generator.addCode(`digitalWrite(10,HIGH);`);
		Generator.addCode(`digitalWrite(11,HIGH);`);
		}else if(`${color}` == 0x0000FF){
		Generator.addCode(`digitalWrite(9,LOW);`);
		Generator.addCode(`digitalWrite(10,LOW);`);
		Generator.addCode(`digitalWrite(11,HIGH);`);
		}else if(`${color}` == 0xFF00FF){
		Generator.addCode(`digitalWrite(9,HIGH);`);
		Generator.addCode(`digitalWrite(10,LOW);`);
		Generator.addCode(`digitalWrite(11,HIGH);`);
		}else if(`${color}` == 0xFFFFFF){
		Generator.addCode(`digitalWrite(9,HIGH);`);
		Generator.addCode(`digitalWrite(10,HIGH);`);
		Generator.addCode(`digitalWrite(11,HIGH);`);
		}else if(`${color}` == 0x000000){
		Generator.addCode(`digitalWrite(9,LOW);`);
		Generator.addCode(`digitalWrite(10,LOW);`);
		Generator.addCode(`digitalWrite(11,LOW);`);
		}		
    }


    //% block="---"
    export function BlankLine01() {
    }


    //% block="读取旋转角度传感器(A0)" blockType="reporter"
    export function GetRotation(parameter: any, block: any) {
        Generator.addCode(`analogRead(A0)`);
        }


    //% block="---"
    export function BlankLine04() {
    }


    //% block="读取光线传感器(A1)" blockType="reporter"
    export function GetLight(parameter: any, block: any) {
        Generator.addCode(`analogRead(A1)`);
        }


    //% block="---"
    export function BlankLine02() {
    }


    //% block="读取LM35温度(A2)" blockType="reporter"
    export function GetLM35(parameter: any, block: any) {
        Generator.addCode(`(125*analogRead(2))>>8`);
    }


    //% block="读取DHT11温湿度传感器(D4)[dpTorH]" blockType="reporter"
    //% dpTorH.shadow="dropdown" dpTorH.options="dpTorH" dpTorH.defl="dropTorH.1"
    export function GetDHT11(parameter: any, block: any) {
        Generator.addInclude('DFRobot_DHT', '#include <DFRobot_DHT.h>');
		Generator.addObject(`dht11`, `DFRobot_DHT`, `dht11;`);
		Generator.addSetup("dht11.begin", `dht11.begin(4, DHT11);`);
		let dptorh = parameter.dpTorH.code
		Generator.addCode(`dht11.${droptorh}()`);
        }


    //% block="---"
    export function BlankLine03() {
    }


    //% block="红外初始化(D6)" blockType="command"
    export function IRinit(parameter: any, block: any) {
        Generator.addInclude('IRremote', '#include <IRremote.h>');
		Generator.addObject(`irrecv(6)`, `IRrecv`, `irrecv(6);`);
		Generator.addObject(`results`, `decode_results`, `results`);
		Generator.addSetup("irrecv.enableIRIn",`irrecv.enableIRIn(); `);
        }
   
  
    //% block="接收到IR信号？" blockType="boolean"
    export function IsIrRecved(parameter: any, block: any) {
		Generator.addCode(`irrecv.decode(&results)`);
        }


    //% block="读取红外接收传感器(D6)" blockType="reporter"
    export function GetIRValue(parameter: any, block: any) {
		Generator.addCode(`results.value`);
        }


    //% block="等待接收下一组信号" blockType="command"
    export function GetIrRecv(parameter: any, block: any) {
         Generator.addCode(`irrecv.resume();`);
        }


    //% block="---"
    export function BlankLine04() {
    }


    //% block="[dpBTNOn][dpONorOFF]？" blockType="boolean"
    //% dpBTNOn.shadow="dropdown" dpBTNOn.options="dpBTN" dpBTNOn.defl="dpBTNOn.1"
	//% dpONorOFF.shadow="dropdown" dpONorOFF.options="dpONorOFF" dpONorOFF.defl="dpONorOFF.1"
    export function IsBtnPress(parameter: any, block: any) {
	    let dpbtnon = parameter.dropBTNOn.code
	    let dponoroff = parameter.dropONorOFF.code		
	    if(dponoroff == 'on')
			Generator.addCode(`(!digitalRead(${dropbtnon}))`);
		else if(dponoroff == 'off')
			Generator.addCode(`digitalRead(${dropbtnon})`);
    }

 
 
     //% block="蜂鸣器D5音调为[noteTone]节拍为[dprBeat]" blockType="command"
    //% noteTone.shadow="note" noteTone.defl=131
    //% dprBeat.shadow="dropdownRound" dprBeat.options="dprBeat" drpBeat.defl="dprBeat.1"
    export function Buzzer(parameter: any, block: any) {
        let notetone = parameter.noteTone.code
        let dprbeat = parameter.dprBeat.code
        Generator.addInclude('DFRobot_Libraries', '#include <DFRobot_Libraries.h>');
        Generator.addObject(`DFTone`, `DFRobot_Tone`, `DFTone;`);
        Generator.addCode(`DFTone.play(5, ${notetone}, ${dprbeat});`);
    }
 
 
 }
